<template>
  <div>
      <el-button type="primary" class="xuan" @click="xuan">删除选中</el-button>
    <el-table
    class="tab"
      ref="multipleTable"
      :data="reslist"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="sel"
    >
      <el-table-column type="selection" width="60"> </el-table-column>
      <el-table-column
        prop="nickname"
        label="姓名
      "
        width="120"
      >
      </el-table-column>
      <el-table-column label="头像" width="150">
        <template slot-scope="scope">
          <el-image
            style="width: 60px; height: 60px; border-radius: 50%"
            :src="scope.row.avatar"
            :preview-src-list="imglist"
            @click="imglist.push(scope.row.avatar)"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column
        prop="updated_at"
        label="手机号
      "
        width="160"
      >
      </el-table-column>
      <el-table-column
        prop="created_at"
        label="时间
      "
        width="160"
      >
      </el-table-column>
      <el-table-column
        prop="adress"
        width="260"
        label="地址"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" @click="bian(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
          @click="del(scope.$index)" 
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      class="fy"
      :current-page="pageNum"
      :page-size="pageSize"
      background
      layout="prev, pager, next"
      :total="list.length"
      @current-change="change"
    >
    </el-pagination>
    <!-- 编辑静态框 -->
       <el-dialog title="编辑信息" :visible.sync="showEdit">
      <el-form :model="edit">
        <el-form-item label="用户名">
          <el-input v-model="edit.nickname"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="edit.adress"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showEdit = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
       </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      //所有的数据
      list: [],
      // 查看大图
      imglist: [],
      //分页跳过多少页
      pageNum: 1,
      //  分页的条数
      pageSize: 5,
         // 编辑
      edit: {
        id:"",
        nickname: "",
        adress: "",
      },
      // 控制编辑弹框出现的变量
      showEdit: false,
    };
  },
  mounted() {
    this.$axios.get("/list.json").then((res) => {
      this.list = res.data;
    });
  },
  // 分页
  computed: {
    reslist() {
      return this.list.slice(
        this.pageSize * (this.pageNum - 1),
        this.pageSize * this.pageNum
      );
    },
  },
  methods: {
    // 分页
    change(val) {
      this.pageNum = val;
    },
    // 删除
    del(index) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.list.splice(index, 1);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //  编辑
    bian(item) {
      this.showEdit = true;
      this.edit.nickname = item.nickname;
      this.edit.adress = item.adress;
      this.edit.id = item.uid;
    },
    // 保存编辑
    update() {
      this.showEdit = false;
      this.list.forEach((item) => {
        if (item.uid == this.edit.id) {
          item.nickname = this.edit.nickname;
          item.adress = this.edit.adress;
        }
      });
    },   // 把所有要选中删除的id存起来存成一个字符串
    sel(val) {
      let res = val.map((item) => {
        return item.uid;
      });
      this.removeIds = res.join(",");
    },
        // 选中删除
    xuan() {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.list = this.list.filter((item) => {
            if (this.removeIds.indexOf(item.uid) == -1) {
              return item;
            }
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
<style  scoped>
.fy {
  position: fixed;
  top: 670px;
  left: 220px;
}
.xuan{
  float: left;
}
.tab{
  float: left;
  margin-top: 10px;
}
</style>